package tWeight;

import java.io.IOException;
import java.util.Properties;
import java.util.Stack;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;

import matrics.SparseVector;
import myLucene.GetTime;
import myLucene.Props;

public class TfIdf {
	public static SparseVector getTfIdf(int[] tf, IndexReader iReader,
			Stack<Integer> stops) throws IOException {
		long timeS = System.currentTimeMillis();
		System.out.println("Start TfIdf calculation ... " + GetTime.getTime());

		SparseVector sp = new SparseVector(Props.dictSize);

		for (int term = 0; term < tf.length; term++) {
			if (!stops.isEmpty()) {
				if (stops.peek() == term) {
					stops.pop();
					sp.put(term, Props.zero);
					continue;
				}
			}
			if (tf[term] > 0) {
				double idf = Math.log10((double) iReader.maxDoc()
						/ (double) iReader.docFreq(new Term(Props.queryField,
								String.valueOf(term))));

				// double test = iReader.maxDoc()
				// / iReader.docFreq(new Term(Props.queryField, String
				// .valueOf(term)));

				sp.put(term, idf * tf[term]);
				// System.out.println("TfIdf : "
				// + intToTerms[term]
				//
				// + " >"
				// + idf
				// + " tf>"
				// + tf.get(term)
				// + " "
				// + iReader.docFreq(new Term(Props.queryField, String
				// .valueOf(term))) + " " + iReader.maxDoc());
			} else {
				sp.put(term, Props.zero);
			}
		}
		System.out.println("TfIdf calculation done, time needed: "
				+ GetTime.getDuration(timeS, System.currentTimeMillis()));
		return sp;

	}
}
